/*
 * Copyright (c) 2011 Seoul National University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jung Ho Ahn  ( gajh@snu.ac.kr )
 *
 */

#include "KmerNode.h"
#include "Kmer.h"
#include <cassert>


extern ostream & operator<<(ostream &out, nucleotide_type n);

ostream & operator<<(ostream & out, const KmerNode & k)
{
  assert(k.kmer);
  if (k.isKmerRC == true)
  {
    Kmer kmerRC(*(k.kmer));
    kmerRC.reverseComplement();
    out << k.type << k.id << ": " << kmerRC << " ";
  }
  else
  {
    out << k.type << k.id << ": " << *(k.kmer) << " ";
  }
  out << (k.has_branch ? "has" : "no") << "Branch, hitcount= " << k.hitcount;
  out << ", numPrev = " << k.numPrev;
  out << ", numNext = " << k.numNext;
  out << ", prev_node = " << k.prev_type;
  out << ", next_node = " << k.next_type << endl;

  for (int i = 0; i < 4; i++)
  {
    out << "prev" << Kmer::nucleotideToChar((nucleotide_type) i) << "= ";
    if (k.hasBranch() == false && k.prev_type == (nucleotide_type) i)
    {
      out << k.prev.node->type << k.prev.node->id;
    }
    else if (k.hasBranch() == true && k.prev.nodes[i].node != NULL)
    {
      out << k.prev.nodes[i].node->type << k.prev.nodes[i].node->id << " " << k.prev.nodes[i].hitcount;
    }
    else
    {
      out << "NULL";
    }

    out << ", next" << Kmer::nucleotideToChar((nucleotide_type) i) << "= ";
    if (k.hasBranch() == false && k.next_type == (nucleotide_type) i)
    {
      out << k.next.node->type << k.next.node->id;
    }
    else if (k.hasBranch() == true && k.next.nodes[i].node != NULL)
    {
      out << k.next.nodes[i].node->type << k.next.nodes[i].node->id << " " << k.next.nodes[i].hitcount;
    }
    else
    {
      out << "NULL";
    }
    out << endl;
  }
  return out;
}


KmerNode::KmerNode(Kmer * _kmer, uint64_t id_, uint64_t hitcount_, bool isKmerRC_)
 :Node(ndt_kmer, id_), kmer(_kmer), hitcount(hitcount_), prev_type(nt_I), next_type(nt_I),
  numPrev(0), numNext(0), isKmerRC(isKmerRC_),
  prev((Node *)NULL), next((Node *)NULL)
{
}


KmerNode::~KmerNode()
{
  if (has_branch == true)
  {
    delete [] prev.nodes;
    delete [] next.nodes;
  }
}


Node* KmerNode::getPrev(nucleotide_type nucleotide)
{
  assert(nucleotide != nt_I);
  return (has_branch == false) ? prev.node : prev.nodes[nucleotide].node;
}


Node* KmerNode::getNext(nucleotide_type nucleotide)
{
  assert(nucleotide != nt_I);
  return (has_branch == false) ? next.node : next.nodes[nucleotide].node;
}


void KmerNode::setPrev(Node * node, nucleotide_type nucleotide)
{
  assert(nucleotide != nt_I);

  if (has_branch == false)
  {
    if (prev_type != nt_I && prev_type != nucleotide)
    {
      has_branch = true;
      NodeHitcountPair * nodes = new NodeHitcountPair[4];
      nodes[prev_type].node = prev.node;
      prev.nodes = nodes;
      numPrev = 1;
      numNext = 0;
      nodes = new NodeHitcountPair[4];
      if (next_type != nt_I)
      {
        nodes[next_type].node = next.node;
        numNext++;
      }
      next.nodes = nodes;
      //valid = true;
    }
    else
    {
      numPrev = 1;
      prev_type       = nucleotide;
      prev.node       = node;
      return;
    }
  }
  prev_type                  = nucleotide;
  numPrev += (prev.nodes[nucleotide].node == NULL) ? 1 : 0;
  prev.nodes[nucleotide].node = node;
}



void KmerNode::setNext(Node * node, nucleotide_type nucleotide)
{
  assert(nucleotide != nt_I);

  if (has_branch == false)
  {
    if (next_type != nt_I && next_type != nucleotide)
    {
      has_branch = true;
      NodeHitcountPair * nodes = new NodeHitcountPair[4];
      nodes[next_type].node = next.node;
      next.nodes = nodes;
      numNext = 1;
      numPrev = 0;
      nodes = new NodeHitcountPair[4];
      if (prev_type != nt_I)
      {
        nodes[prev_type].node = prev.node;
        numPrev++;
      }
      prev.nodes = nodes;
      //valid = true;
    }
    else
    {
      numNext = 1;
      next_type = nucleotide;
      next.node = node;
      return;
    }
  }
  next_type = nucleotide;
  numNext += (next.nodes[nucleotide].node == NULL) ? 1 : 0;
  next.nodes[nucleotide].node = node;
  assert(next.nodes[nucleotide].node == node);
}


